﻿using ERPPublic;
using ERPPublic.eneity;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using WarehouseManagement.Application.Command.WarehouseCommand;
using WarehouseManagement.Domain.DTO.Warehouse;
using WarehouseManagement.Domain.Entity;
namespace WarehouseManagement.Controllers
{
    /// <summary>
    /// 仓库管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class WarehouseController : ControllerBase
    {
        protected readonly IMediator mediatorp;
        public WarehouseController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }
        /// <summary>
        /// 仓库添加
        /// </summary>
        /// <param name="cd">要添加的实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> WarehouseAdd(WarehouseCreateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.addWarehouseDTO.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.addWarehouseDTO.AddName = name;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                cd.addWarehouseDTO.User_Id = int.Parse(userId);
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 仓库信息展示
        /// </summary>
        /// <param name="cd">查询条件</param>
        /// <returns>返回集合</returns>
        [HttpGet]
        public async Task<ApiResult<Warehousepage>> ShowWarehouse([FromQuery]WarehouseSelectCommand cd)
        {
            ApiResult<Warehousepage> result = new ApiResult<Warehousepage>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.Office_Id = sid;
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 仓库删除
        /// </summary>
        /// <param name="command">要删除的信息id</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelWarehouse(WarehouseDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.DelName = name;
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 反填仓库
        /// </summary>
        /// <param name="command">要反填的信息id</param>
        /// <returns>返回满足条件的数据</returns>
        [HttpGet]
        public async Task<ApiResult<UpdFanWarehouseDTO>> FillWarehouse([FromQuery] WarehouseFanCommand command)
        {
            ApiResult<UpdFanWarehouseDTO> result = new ApiResult<UpdFanWarehouseDTO>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 仓库修改
        /// </summary>
        /// <param name="command">要修改的实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UptWarehouse(WarehouseUpdCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.updDto.UpdName = name;
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.updDto.Office_Id = sid;
                result.Data = await mediatorp.Send(command);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }


      
    }
}
